package ntd.util.util;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import ntd.util.UtilCatch;
import ntd.util.config.common.bo.ComDictVo;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;

public class UtilEnum {

    public static List<ComDictVo> generateDictEnum(Class type) {
        if (ObjectUtil.isNull(type)) {
            return ListUtil.empty();
        }
        Method getName = ReflectUtil.getMethodByName(type, "getName");
        Method getMemo = ReflectUtil.getMethodByName(type, "getMemo");
        Collection values = EnumUtil.getEnumMap(type).values();
        return CollStreamUtil.toList(values, obj -> {
            ComDictVo comDictVo = new ComDictVo();
            Opt.ofNullable(getName).ifPresent(method -> comDictVo.setName(UtilCatch.tryCatch(() -> StrUtil.toString(method.invoke(obj)))));
            Opt.ofNullable(getMemo).ifPresent(method -> comDictVo.setMemo(UtilCatch.tryCatch(() -> StrUtil.toString(method.invoke(obj)))));
            comDictVo.setVal(String.valueOf(obj));
            return comDictVo;
        });
    }
}
